﻿@using Kite.Gateway.Application.Contracts
@using Kite.Gateway.Application.Contracts.Dtos.Middleware
@inject ToastService ToastService
@inject IMiddlewareAppService MiddlewareAppService

<div class="row">
    <ValidateForm Model="@Model" OnValidSubmit="@OnSaveAsync" OnInvalidSubmit="@OnInvalidSubmitAsync">
            <div class="col-12">
                   <BootstrapInput @bind-Value="Model.Name" DisplayText="中间件名称" IsTrim="true" />
             </div>
             <div class="col-12"  style="margin-top:5px;" >
                   <BootstrapInput @bind-Value="Model.Server" DisplayText="服务端地址" IsTrim="true" />
             </div>
             <div class="col-12"  style="margin-top:5px;" >
                   <Select @bind-Value="Model.SignalType" DisplayText="通信类型">
                   </Select>
             </div>
            <div class="col-12"  style="margin-top:5px;" >
                   <BootstrapInputNumber  @bind-Value="Model.ExecWeight" DisplayText="执行权重" IsTrim="true" />
             </div>
            <div class="col-12"  style="margin-top:5px;">
                   <Switch OnColor="Color.Success" @bind-Value="Model.UseState" DisplayText="状态" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-12"  style="margin-top:5px;" >
                 <Textarea  @bind-Value="Model.Description" DisplayText="描述" IsTrim="true"/>
            </div>
            <div class="row" style="margin-top:10px;">
                <div class="col-12">
                    <Button ButtonType="ButtonType.Submit"  IsAsync="true" Color="Color.Primary">保 存</Button>
                </div>
            </div>
           </ValidateForm>
</div>


@code {
    
    [Parameter]
    public EventCallback OnClickCallback { get; set; }
    //表单数据
    private CreateMiddlewareDto Model { get; set; }
    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        Model = new CreateMiddlewareDto()
        {
            UseState=true
        };
    }
    private Task OnInvalidSubmitAsync(EditContext context)
    {
        return Task.CompletedTask;
    }
    private async Task OnSaveAsync(EditContext context)
    {
        var result= await MiddlewareAppService.CreateAsync(Model);
        if (result.Code != 0)
        {
            await ToastService.Warning(result.Message);
        }
        else
        {
            await OnClickCallback.InvokeAsync();
            await ToastService.Success("保存成功");
            Model = new CreateMiddlewareDto()
            {
                UseState=true
            };
        }
    }
}
